<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true">
            <el-form-item label="保养计划编号">
                <el-input v-model="queryParams.code" placeholder="请输入保养计划编号" clearable style="width: 200px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="保养计划名称">
                <el-input v-model="queryParams.name" placeholder="请输入保养计划名称" clearable style="width: 200px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="保养计划类型">
                <el-select v-model="queryParams.ctype" placeholder="请选择保养计划类型" clearable>
                    <el-option v-for="dict in mes_care_plantype" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <!-- 暂时不做 -->
                <!-- <el-button type="primary" plain icon="Download" @click="handleExport">导出</el-button> -->
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" border :data="supplierList" style="margin-top: 20px;">
            <el-table-column label="序号" width="60" type="index" align="center">
            </el-table-column>
            <el-table-column label="状态" align="center" prop="status">
                <template #default="scope">
                    <dict-tag :options="mes_people_status" :value="scope.row.status" />
                </template>
            </el-table-column>
            <el-table-column label="保养计划编号" align="center" prop="code" width="200">
                <template v-slot="{ row }">
                    <el-button type="primary" @click="handleView(row)" text>{{ row.code
                    }}</el-button>
                </template>
            </el-table-column>
            <el-table-column label="保养计划名称" width="120" align="center" prop="name" />
            <el-table-column label="保养计划类型" align="center" prop="ctype">
                <template #default="scope">
                    <dict-tag :options="mes_care_plantype" :value="scope.row.ctype" />
                </template>
            </el-table-column>
            <el-table-column label="执行周期计划" align="center" prop="cycleRule" />
            <el-table-column label="执行周期描述" align="center" prop="cycleRuleDes" />
            <el-table-column label="计划开始时间" width="120" align="center" prop="startTime">
                <template #default="scope">
                    <span>{{ scope.row.startTime}}</span>
                </template>
            </el-table-column>
            <el-table-column label="计划结束时间" width="120" align="center" prop="endTime">
                <template #default="scope">
                    <span>{{ scope.row.endTime}}</span>
                </template>
            </el-table-column>

            <el-table-column label="是否有明细" align="center" prop="hasDetail">
                <template #default="scope">
                    <span>{{ scope.row.hasDetail == 1 ? '是' : '否' }}</span>
                </template>
            </el-table-column>
            <!-- <el-table-column label="下发部门编号" align="center" prop="deptCode" /> -->
            <el-table-column label="下发部门名称" align="center" width="120" prop="deptName" />
            <!-- <el-table-column label="周月计划扩展" align="center" prop="planMultiple" /> -->
          
            <el-table-column label="备注" align="center" prop="remark" />

            <el-table-column label="操作" fixed="right" align="center" min-width="150" class-name="small-padding fixed-width">
                <template #default=scope>
                    <el-tooltip content="编辑" v-if="scope.row.hasDetail == 0" placement="top">
                        <el-button link type="primary" icon="Edit" @click="handleEdit(scope.row)"></el-button>
                    </el-tooltip>
                    <el-tooltip content="删除" placement="top">
                        <el-button link type="primary" icon="Delete" @click="handleDle(scope.row)"></el-button>
                    </el-tooltip>
                    <el-button link type="primary" icon="TurnOff" @click="handleService(scope.row)">{{ scope.row.status == 0
                        ? '停用' : '启用' }}</el-button>
                    <el-button link type="primary" v-if="scope.row.hasDetail == 0" icon="Connection"
                        @click="addRecords(scope.row)">添加记录</el-button>
                    <el-button link type="primary" v-else icon="Tickets" @click="handleRecords(scope.row)">保养记录</el-button>

                </template>
            </el-table-column>
        </el-table>
        <pagination style="margin-top: 0;padding-top: 0;" v-show="total > 0" :total="total"
            v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />


    </div>
</template>
 
<script setup name="plan">
import { listCare, delCare, addDetail, changeMesCareStatus } from '@/api/maintenance/plan.js'

const { proxy } = getCurrentInstance();
import { getCare } from '@/api/maintenance/plan.js'
const { mes_care_plantype, mes_people_status } = proxy.useDict('mes_care_plantype', 'mes_people_status');
import { downLoadBlob } from '@/utils/ruoyi'
import { ElMessage, ElMessageBox } from 'element-plus'
const router = useRouter()
const supplierList = ref([]);
const loading = ref(false);
const total = ref(0);
const showSearch = ref(true);

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        ctype: null
    },
    rules: {
        code: [
            { required: true, message: "客户编码不能为空", trigger: "blur" }
        ],
        name: [
            { required: true, message: "客户名称不能为空", trigger: "blur" }
        ],
    }
});
const { queryParams, form, rules } = toRefs(data);
// 查看
const handleView = (row) => {
    router.push({
        path: '/maintenance/plan/details',
        query: {
            id: row.id,
            readonly: true,
            code: row.code,
            status: row.status
        }
    })
}
// 新增
const handleAdd = () => {
    router.push('/maintenance/plan/details')
}
const handleEdit = (row) => {
    router.push({
        path: '/maintenance/plan/details',
        query: {
            id: row.id,
            code: row.code,
            status: row.status
        }
    })
}
//停用启用
const handleService = (row) => {
    const state = row.status === 0 ? '1' : '0';
    const stateText = row.status === 0 ? '停用' : '启用';
    ElMessageBox.confirm(`确定要 ${stateText} 名称为 ${row.name} 的保养计划吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(() => {
        // 用户点击了确定按钮
        changeMesCareStatus(row.id, state,row.code ,row.ctype).then(response => {
            if (response.code === 200) {
                ElMessage({
                    message: `${stateText}成功`,
                    type: 'success',
                });
                getList();
            }
        });
    })
};

//删除
const handleDle = (row) => {
    ElMessageBox.confirm('确定要删除名称为 ' + row.name + ' 的保养计划吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(() => {
        // 用户点击了确定按钮
        delCare(row.id).then(response => {
            if (response.code === 200) {
                ElMessage({
                    message: '删除成功',
                    type: 'success',
                });
                getList();
            }
        });
    }).catch(() => {
        //取消
    });
};
const addRecords = (row) => {
    console.log('row', row)
    getCare(row.id).then(response => {
        console.log('response1', response)
        row.hasDetail = 1
        row.mesMachineCarereationList = response.data.mesMachineCarereationList
    }).then(response => {
        addDetail(row).then(response => {
            console.log('response2', response)
            if (respoonse.code == 200) {
                ElMessage({
                    message: '添加成功',
                    type: 'success',
                });
                getList()
            }
        })
    })

}
const handleRecords = (row) => {

    router.push({
        path: '/maintenance/plan/planRecord',
        query: {
            id: row.id,
            code: row.code,
        }
    })
}
function getList() {
    loading.value = true;
    listCare(queryParams.value).then(response => {
        supplierList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}
/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}
/** 重置按钮操作 */
function resetQuery() {
    queryParams.value.code = null
    queryParams.value.name = null
    queryParams.value.ctype = null
    handleQuery();
}
// 导出
const handleExport = () => {
    // customerExport(queryParams.value).then(res => {
    //     downLoadBlob(res, '客户列表')
    // })
}
getList();
</script>
 